import json
from datetime import datetime
from django.core.paginator import Paginator
from django.http import JsonResponse
from django.views import View

from lapi_app.models.task_case.task import Task


class GetTask(View):

    def post(self, request, *args, **kwargs):
        data = json.loads(request.body)
        if data["task_name"] == "":
            tasks = Task.objects.all().order_by("-create_time")
            size_page = data["size_page"]
            current_pages = data["current_page"]
            case_total = len(tasks)
            task_list = []
            try:
                for task in tasks:
                    task_dict = {
                        "task_id": task.id,
                        "task_name": task.task_name,
                        "task_cases": task.task_case,
                        "status": task.task_status,
                        "task_type": task.task_type,
                        "run_type": task.run_type,
                        "result_id": task.result_id,
                        "active": task.active,
                        "username": task.user.username,
                        "task_description": task.task_description,
                        "update_time": datetime.strftime(task.update_time, "%Y-%m-%d %H:%M:%S"),
                        "create_time": datetime.strftime(task.create_time, "%Y-%m-%d %H:%M:%S")
                    }
                    task_list.append(task_dict)
                p = Paginator(task_list, size_page)
                page1 = p.page(current_pages)
                current_page = page1.object_list
                return JsonResponse({
                    "code": 200,
                    "message": "获取任务列表成功",
                    "content": current_page,
                    "case_total": case_total,
                    "size_page": size_page,
                    "current_page": current_pages
                })
            except Exception as e:
                return JsonResponse({
                    "code": 100,
                    "message": "获取任务列表失败",
                    "Exception": str(e)
                })
        else:
            task_name = data["task_name"]
            tasks = Task.objects.filter(task_name__contains=task_name).order_by("-create_time")
            size_page = data["size_page"]
            current_pages = data["current_page"]
            case_total = len(tasks)
            task_list = []
            try:
                for task in tasks:
                    task_dict = {
                        "task_id": task.id,
                        "task_name": task.task_name,
                        "task_cases": task.task_case,
                        "status": task.task_status,
                        "task_type": task.task_type,
                        "result_id": task.result_id,
                        "active": task.active,
                        "username": task.user.username,
                        "task_description": task.task_description,
                        "update_time": datetime.strftime(task.update_time, "%Y-%m-%d %H:%M:%S"),
                        "create_time": datetime.strftime(task.create_time, "%Y-%m-%d %H:%M:%S")
                    }
                    task_list.append(task_dict)
                p = Paginator(task_list, size_page)
                page1 = p.page(current_pages)
                current_page = page1.object_list
                return JsonResponse({
                    "code": 200,
                    "message": "获取任务列表成功",
                    "content": current_page,
                    "case_total": case_total,
                    "size_page": size_page,
                    "current_page": current_pages
                })
            except Exception as e:
                return JsonResponse({
                    "code": 100,
                    "message": "获取任务列表失败",
                    "Exception": str(e)
                })
